#include "HttpClient.h"



HttpClient::HttpClient(QObject *parent):QObject(parent)
{
    manager = new QNetworkAccessManager();
}

void HttpClient::PostAysnc(const QString &url, const QJsonObject &data)
{
    QNetworkRequest request;
    request.setUrl(QUrl(url));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    QJsonDocument doc(data);
    QByteArray postData = doc.toJson();

    QNetworkReply *reply = manager->post(request, postData);

    // 连接信号，异步处理响应
    connect(reply, &QNetworkReply::finished, this, [this, reply]() {
        HandleResponse(reply);
    });

    // 错误处理
}

void HttpClient::HandleResponse(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError) {
        QByteArray response = reply->readAll();
        emit responseReceived(response);
    } else {
        QString errorString = reply->errorString();
        emit errorOccurred(errorString);
    }

    reply->deleteLater();
}

